<?php
 header("content-type:text/html;charset=utf-8");

include("Template.php") ;  

  
$table=  @$_POST["table"];
$trueTableName=ReplaceTableName( $_POST["table"],$DbPrefix);

echo TableList($db);   

function TableList($tablename)
{
   $tbllist="<form method=\"post\"><select id=\"table\"  name=\"table\">";
   $rs= mysql_list_tables($tablename);
   
   while($row=mysql_fetch_row($rs))
   {
       
      $tbllist.="<option value=\"{$row[0]}\">{$row[0]}</option>\r";   
   }
   
   return $tbllist."</select><input type=\"submit\" value=\"生成\"></form>";
}


function FieldList($tablename)
{
       $result= mysql_query("show full fields from $tablename");
    if (!$result) 
    {
    echo 'Could not run query: ' . mysql_error();
    exit;
   }
    if (mysql_num_rows($result) > 0)
     {

        while ($row = mysql_fetch_assoc($result)) 
        {
               // print_r($row);
              $fieldlist.=ListColum($row); 
               
        }
              
        return $fieldlist;

    }
}

function ShowFieldList($tablename)
{
       $result= mysql_query("show full fields from $tablename");
    if (!$result) 
    {
    echo 'Could not run query: ' . mysql_error();
    exit;
   }
    if (mysql_num_rows($result) > 0)
     {
         $header.="<tr>"; 
        while ($row = mysql_fetch_assoc($result)) 
        {
               // print_r($row);
              $fieldlist.=ShowListColum($row);
              if(($row["Comment"]=="")||($row["Comment"]==null)) 
              $header.="<td>{$row['Field']}</td>";
              else
              $header.="<td>{$row['Comment']}</td>"; 
               
        }
        $header.="</tr>"; 
              
             
        return "\t\t<tr>".$fieldlist."<td><input type=\"button\" value=\"编辑\" class=\"small button\" onClick=\"edit({\$vo.id})\"> <input type=\"button\" value=\"删除\" class=\"small button\" onClick=\"del({\$vo.id})\"></td></tr>";

    }
}



function ShowFormHeader($tablename)
{
       $result= mysql_query("show full fields from $tablename");
    if (!$result) 
    {
    echo 'Could not run query: ' . mysql_error();
    exit;
   }
    if (mysql_num_rows($result) > 0)
     {
         $header.="<tr>"; 
        while ($row = mysql_fetch_assoc($result)) 
        {
               // print_r($row);
              $fieldlist.=ShowListColum($row);
              if(($row["Comment"]=="")||($row["Comment"]==null)) 
              $header.="<th><a  href=\"javascript:sortBy('{$row['Field']}')\">{$row['Field']}</a></th>";
              else
              $header.="<th><a  href=\"javascript:sortBy('{$row['Field']}')\">{$row['Comment']}</a></th>"; 
               
        }
        $header.="<th colspan=20>操作</th></tr>"; 
              
             
        return $header;

    }
}


function ShowListColum($colum)
{
   $fieldname= $colum["Field"];
   $Default= $colum["Default"];
    $fieldtype=strtolower( preg_replace("/\([\s\S].*\)/","",  $colum["Type"]));
        switch($fieldtype)
        {


            

            default:
            return "\n\t\t\t<td>{\$vo.$fieldname}</td><!--\n\t\t\t<td>$fieldname</td>\n\t\t-->\n"; 
            break;
             
        } 
        
        
    
}

function EditFieldList($tablename)
{
       $result= mysql_query("show full fields from $tablename");
    if (!$result) 
    {
    echo 'Could not run query: ' . mysql_error();
    exit;
   }
    if (mysql_num_rows($result) > 0)
     {

        while ($row = mysql_fetch_assoc($result)) 
        {
               // print_r($row);
              $fieldlist.=EditListColum($row); 
               
        }
              
        return $fieldlist;

    }
}

function EditListColum($colum)
{
   $fieldname= $colum["Field"];
   $Default= $colum["Default"];
    $fieldtypes= preg_split("/\s/",preg_replace("/\([\s\S].*\)/","",  $colum["Type"]));
    $fieldtype=$fieldtypes[0];
            switch($fieldtype)
        {


            default:
            if(($colum["Comment"]=="")||($colum["Comment"]==null))
            return "\t\t<tr>\n\t\t\t<td class=\"tRight\" width=\"30%\">$fieldname</td>\n\t\t\t<td><input class=\"medium bLeft\" name=\"$fieldname\" type=\"text\" value=\"{\$vo.$fieldname}\" ></td><!--\n\t\t\t<td>$fieldname</td>\n\t\t--></tr>\n"; 
            else
            return "\t\t<tr>\n\t\t\t<td class=\"tRight\" width=\"30%\">{$colum["Comment"]}</td>\n\t\t\t<td><input class=\"medium bLeft\" name=\"$fieldname\" type=\"text\" value=\"{\$vo.$fieldname}\" ></td><!--\n\t\t\t<td>$fieldname</td>\n\t\t--></tr>\n"; 
            break;
             
        } 
        
        
    
}


function ListColum($colum)
{
   $fieldname= $colum["Field"];
   $Default= $colum["Default"];
    $fieldtype= preg_replace("/\([\s\S].*\)/","",  $colum["Type"]);
            switch($fieldtype)
        {

            case 'text':
            case 'longtext':
            case 'tinytext':
            if(($colum["Comment"]=="")||($colum["Comment"]==null))
            return "\t\t<tr>\n\t\t\t<td class=\"tRight\" width=\"30%\">$fieldname</td>\n\t\t\t<td><textarea id=\"$fieldname\" style=\"width:700px;height:300px\" name=\"$fieldname\">{\$vo.$fieldname}</textarea></td><!--\n\t\t\t<td>$fieldname</td>\n\t\t--></tr>\n"; 
            else
            return "\t\t<tr>\n\t\t\t<td class=\"tRight\" width=\"30%\">{$colum["Comment"]}</td>\n\t\t\t<td><textarea style=\"width:700px;height:300px\" id=\"$fieldname\" name=\"$fieldname\">{\$vo.$fieldname}</textarea></td><!--\n\t\t\t<td>$fieldname</td>\n\t\t--></tr>\n"; 
            break;


            default:
            if(($colum["Comment"]=="")||($colum["Comment"]==null))
            return "\t\t<tr>\n\t\t\t<td class=\"tRight\" width=\"30%\">$fieldname</td>\n\t\t\t<td><input name=\"$fieldname\" type=\"text\" value=\"{\$vo.$fieldname}\" ></td><!--\n\t\t\t<td>$fieldname</td>\n\t\t--></tr>\n"; 
            else
            return "\t\t<tr>\n\t\t\t<td class=\"tRight\" width=\"30%\">{$colum["Comment"]}</td>\n\t\t\t<td><input name=\"$fieldname\" type=\"text\" value=\"{\$vo.$fieldname}\" ></td><!--\n\t\t\t<td>$fieldname</td>\n\t\t--></tr>\n"; 
            break;
             
        } 
        
        
    
}



if($table){
    
 createDir($ViewBasePath.$trueTableName);
 $ViewPath=$ViewBasePath.$trueTableName."/";   
 $tempReplace=$AddTemplate;
 $tempReplace=TemplateReplace('Form',$trueTableName,$tempReplace);   
    
echo "<textarea cols=\"120\" rows=\"80\">".FieldList($table)."</textarea>";
echo FieldList($table);
// WriteFile($ViewPath.'add'.$trueTableName.".html",TemplateReplace('FormList',FieldList($table),$tempReplace));

 WriteFile($ViewPath.'add'.".html",TemplateReplace('FormList',FieldList($table),$tempReplace));
 
  $tempReplace=$EditTemplate;
 $tempReplace=TemplateReplace('Form',$trueTableName,$tempReplace); 
 
//  WriteFile($ViewPath.'edit'.$trueTableName.".html",TemplateReplace('EditList',EditFieldList($table),$tempReplace));

  WriteFile($ViewPath.'edit'.".html",TemplateReplace('EditList',EditFieldList($table),$tempReplace));  
 
 $tempReplace=$ListTemplate;
 
  $tempReplace=TemplateReplace('Form',$trueTableName,$tempReplace);  
   $tempReplace=TemplateReplace('Header',ShowFormHeader($table),$tempReplace);
 
 //  WriteFile($ViewPath.'show'.$trueTableName.".html",TemplateReplace('ListResult',ShowFieldList($table),$tempReplace));
 
  WriteFile($ViewPath.'index'.".html",TemplateReplace('ListResult',ShowFieldList($table),$tempReplace));
 
 
 
 
  $tempReplace=$FlexGridTemplate;
 
  $tempReplace=TemplateReplace('Form',$trueTableName,$tempReplace);  
   $tempReplace=TemplateReplace('FieldSearchItems',FieldSearchItems($table),$tempReplace);

 //  WriteFile($ViewPath.'show'.$trueTableName.".html",TemplateReplace('ListResult',ShowFieldList($table),$tempReplace));
 
  WriteFile($ViewPath.'view'.".html",TemplateReplace('FieldColModel',FieldColModel($table),$tempReplace));
 
 
   WriteFile($ViewPath.'delete'.".html",$DeleteTemplate);
   
   
 
 
}


?>
